<template>
  <div class="app-container">
    <div class="container">
      <!--工具栏-->
      <el-form :inline="true" size="small" class="demo-form-inline" label-position="right">
        <el-form-item label="创建时间:">
          <el-date-picker
            v-model="query.time"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd HH:mm"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            style="width:360px"/>
        </el-form-item>
        <el-form-item label="客户编号:">
          <el-input
            v-model="query.user_id"
            clearable
            class="filter-item"
            @keyup.enter.native="toQuery"
          />
        </el-form-item>
        <el-form-item label="手机号码:">
          <el-input
            v-model="query.phone"
            clearable
            class="filter-item"
            @keyup.enter.native="toQuery"
          />
        </el-form-item>
        <el-form-item label="交易编号:">
          <el-input
            v-model="query.account_no"
            clearable
            class="filter-item"
            @keyup.enter.native="toQuery"
          />
        </el-form-item>
        <el-form-item label="收支类型:">
          <el-select
            v-model="query.happen_proceed"
            clearable
            class="filter-item"
          >
            <el-option
              v-for="(item,key) in queryHappenOptions"
              :key="key"
              :label="item"
              :value="key"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="订单编号:">
          <el-input
            v-model="query.order_no"
            clearable
            class="filter-item"
            @keyup.enter.native="toQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="filter-item"
            size="mini"
            type="success"
            icon="el-icon-search"
            @click="toQuery"
          >查询</el-button>
          <el-button
            class="filter-item"
            size="mini"
            icon="el-icon-refresh"
            @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="operate-container">
        <div />
        <div>
          <el-button class="filter-item" type="primary" size="mini" @click="exportSearch" >导出（查询）</el-button>
          <el-button class="filter-item" type="warning" size="mini" @click="exportChecked" >导出（勾选）</el-button>
        </div>
      </div>
      <!--表格渲染-->
      <el-table v-loading="loading" ref="table" :data="data" :fit="true" size="small">
        <el-table-column type="selection"/>
        <el-table-column label="客户编号" prop="user_id"/>
        <el-table-column prop="phone" min-width="120px" label="手机号码"/>
        <el-table-column prop="nickname" min-width="120px" show-overflow-tooltip label="微信昵称"/>
        <el-table-column prop="order_no" min-width="120px" label="订单编号"/>
        <el-table-column prop="account_no" min-width="120px" label="交易编号"/>
        <el-table-column prop="happen_proceed" min-width="100px" label="收支类型">
          <template slot-scope="scope">
            <span>{{ happenProceedOptions(scope.row.happen_proceed) }}</span>
          </template>
        </el-table-column>
        <el-table-column :formatter="formatMoney" prop="account_money" min-width="70px" label="金额"/>
        <el-table-column prop="creat_time" min-width="120px" label="创建时间"/>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page="page + 1"
        background
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChange"
        @current-change="pageChange"/>
    </div>
  </div>
</template>

<script>
import initData from '@/mixins/initData'
import { parseTime, parseDate, downLoadExcel } from '@/utils/index'
import { exportIncomeDetails } from '@/api/distributionStatistics'
import Treeselect from '@riophae/vue-treeselect'
import COMMEN from '@/utils/commen'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: { Treeselect },
  mixins: [initData],
  data() {
    return {
      pickerOptions: COMMEN.PICKEROPTIONS,
      loading: false,
      dialog: false,
      company_show: true,
      dialogMessage: '确定要对账吗?',
      queryHappenOptions: COMMEN.HAPPEN_PROCEED,
      queryResultTypeOptions: COMMEN.RESULT_TYPE,
      queryOrderDate: null
    }
  },
  created() {
    this.queryOrderDate = [new Date().setHours(0, 0, 0, 0), new Date()]
    if (this.company_id === 443 || this.company_id === 102) {
      this.company_show = false
    }
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    formatMoney(row, column, cellValue, index) {
      return cellValue.toFixed(2)
    },
    happenProceedOptions(key) {
      return this.queryHappenOptions[key]
    },
    resultTypeOptions(key) {
      return this.queryResultTypeOptions[key]
    },
    parseTime,
    beforeInit() {
      this.url = '/distributior/accountedDetail'
      this.addParams()
      return true
    },
    addParams() {
      const query = this.query
      this.params = { page: this.page, size: this.size }
      const time = query.time
      if (time && time !== null && time !== '' && time !== undefined) {
        this.params['creat_time'] = time.join('~')
      }
      if (query.account_no) {
        this.params['account_no'] = query.account_no
      }
      if (query.happen_proceed) {
        this.params['happen_proceed'] = query.happen_proceed
      }
      if (query.user_id) {
        this.params['user_id'] = query.user_id
      }
      if (query.phone) {
        this.params['phone'] = query.phone
      }
      if (query.result_type) {
        this.params['result_type'] = query.result_type
      }
      if (query.order_no) {
        this.params['order_no'] = query.order_no
      }
    },
    getCustomId(value) {
      for (var i = 0; i < value.length; i++) {
        if (i === (value.length - 1)) {
          return value[i]
        }
      }
    },
    formatOrderTime(timeStamp) {
      const orderTime = new Date(Number(timeStamp))
      return parseDate(orderTime, true)
    },
    formatCoupon(content, key) {
      let [str, str1] = ''
      if (key) {
        str1 = '密钥:' + key
      }
      if (content) {
        str = content + ',' + str1
      } else {
        str = str1
      }
      return str
    },
    formatState(row, column, cellValue, index) {
      return this.queryStateOptions[cellValue]
    },
    formatFinanceState(row, column, cellValue, index) {
      return this.queryFinanceOptions[cellValue]
    },
    formatAccountState(row, column, cellValue, index) {
      return this.queryAccountOptions[cellValue]
    },
    reset() {
      this.query = {}
      this.queryOrderDate = [new Date().setHours(0, 0, 0, 0), new Date()]
      this.init()
    },
    getSelected() {
      const account_no_list = []
      const selectArr = this.$refs.table.selection
      for (let i = 0; i < selectArr.length; i++) {
        account_no_list.push(selectArr[i].account_no)
      }
      return account_no_list
    },
    // 导出勾选
    exportChecked() {
      const account_no_list = this.getSelected()
      this.params = { account_no_list: account_no_list.join(',') }
      exportIncomeDetails(this.params).then((res) => {
        downLoadExcel(res, '收支明细.xls')
      }).catch(err => {
        console.log(err)
      })
    },
    // 导出查询
    exportSearch() {
      this.addParams()
      exportIncomeDetails(this.params).then((res) => {
        downLoadExcel(res, '收支明细.xls')
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style scoped lang="scss">
.el-table-total{
  display: flex;
  font-size: 14px;
  .el-table-total-title{
    color:#000;
    font-weight: 600;
    margin-right: 15px;
    margin-bottom: 10px;
  }
  .el-table-total-name{
    color: rgba(0,0,0,.8);
    margin-right: 20px;
  }
  .el-table-total-number{
    color: red;
  }
}
</style>
